package org.zecontech.extraplus.block;

import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.neoforge.registries.DeferredItem;

// 自定义作物方块
public class CustomCropBlock extends CropBlock {

    private final boolean hasCustomSeeds;
    private final DeferredItem<Item> seedItem;

    // Codec 处理
    public static final MapCodec<CustomCropBlock> CODEC =
            simpleCodec(CustomCropBlock::new);

    // 形状
    private static final VoxelShape[] SHAPES =
            Block.boxes(7, (radius) ->
                    Block.column(16.0F, 0.0F, 2 + radius));

    public CustomCropBlock(BlockBehaviour.Properties properties, boolean hasCustomSeeds, DeferredItem<Item> seedItem) {
        super(properties);
        this.hasCustomSeeds = hasCustomSeeds;
        this.seedItem = seedItem;
    }

    public CustomCropBlock(BlockBehaviour.Properties properties) {
        this(properties, false, null);
    }

    // 形状
    @Override
    protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
        return SHAPES[state.getValue(this.getAgeProperty())];
    }

    // Codec
    @Override
    public MapCodec<? extends CropBlock> codec() {
        return CODEC;
    }

    /**
     * 获取种子
     * @return 种子
     */
    @Override
    protected ItemLike getBaseSeedId() {
        if (hasCustomSeeds) {
            return this.seedItem.get();
        }
        return this.asItem();
    }

    /**
     * 是否有自定义种子
     * @return 是否有自定义种子
     */
    public boolean hasCustomSeeds() {
        return hasCustomSeeds;
    }
}
